<template>
	<div class="suite">
	  <el-form :inline="true" :model="suiteSearch" label-width="100px" style="min-width: 1200px;" class='searchForm'>
		<el-form-item label="套件名称">
			<el-input v-model="suiteSearch.name" placeholder="请输入" clearable/>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" @click="reset">重置</el-button>
			<el-button type="primary" @click="search">搜索</el-button>
		</el-form-item>
	  </el-form>
	  <el-drawer v-model="editDialogVisible" direction="ttb" :show-close="false" :title="title" :size="600">
		  <el-form :model="SuiteSave" label-position='top' label-width="100px">
		  			<el-form-item label="套件名称">
		  				<el-input v-model="SuiteSave.name" autocomplete="off" placeholder="请输入"/>
		  			</el-form-item>
		  			<el-form-item label="套件条件">
		  				<SuiteCondition :keyData='SuiteSave.conditions'></SuiteCondition>
		  			</el-form-item>
		  </el-form>
		  <template #footer>
		  			<span class="dialog-footer">
		  			  <el-button @click="editDialogVisible = false">取消</el-button>
		  			  <el-button type="primary" @click="save">
		  				确认
		  			  </el-button>
		  			</span>
		  </template>
	  </el-drawer>
	  <el-table :data="suite_list.results" class='table'>
		<el-table-column label="序号" width="70" type="index"/>
		<el-table-column label="套件名称" prop="name" min-width="100"/>
		<el-table-column label="创建人" prop="create_by_name"  min-width="70"/>
		<el-table-column label="更新人" prop="update_by_name"  min-width="70"/>
		<el-table-column label="创建时间" prop="create_time" sortable min-width="150"/>
		<el-table-column label="更新时间" prop="update_time" sortable min-width="150"/>
		<el-table-column align="center" min-width="270">
			<template #header>
				<el-button v-if='permission.has_add_permission' type='primary' @click="addSuite">新增</el-button>
			</template>
			<template #default="scope">
			  <el-button v-if="permission.has_edit_permission" :icon="Cpu" size="small" type='primary' @click="run(scope.row)">执行</el-button>
			  <el-button v-if="permission.has_edit_permission" :icon="Edit" size="small" type='primary' @click="editSuite(scope.row)">编辑</el-button>
			  <el-button v-if="permission.has_delete_permission"
				@click="deleteSuite(scope.row.id)"
				size="small"
				type="danger"
			  :icon="Delete"
				>删除</el-button
			  >
			</template>
		</el-table-column>
	  </el-table>
	  <div style="margin: 10px">
	  	<el-pagination
	  	  v-model:current-page="page_size_params.page"
	  	  v-model:page-size="page_size_params.size"
	  	  :page-sizes="[10, 20, 30, 50]"
	  	  :small="small"
	  	  :disabled="disabled"
	  	  :background="background"
	  	  :hide-on-single-page="false"
	  	  layout="total, sizes, prev, pager, next, jumper"
	  	  :total="suite_list.count"
	  	  @size-change="handleSizeChange"
	  	  @current-change="handleCurrentChange"
	  	/>
	  </div>
	</div>
</template>

<script>
import {mapState, mapActions, mapGetters} from 'vuex'
import { ElMessage, ElMessageBox } from 'element-plus'
import { View, Delete, Edit, Plus, EditPen, Cpu} from '@element-plus/icons-vue'
import SuiteCondition from '../../components/SuiteCondition.vue'

export default{
	computed:{
		...mapState(['pathPermission', 'projectInfo', 'env_id']),
	},
	components: {
		SuiteCondition
	},
	data() {
		return {
			permission: {},
			suiteSearch:{
				name: '',
				project: '',
			},
			page_size_params: {
				page: 1,
				size: 10,
			},
			count: 1,
			title: '新增请求头',
			isAdd: true,
			editDialogVisible: false,
			suite_list: [],
			plant_module_list: [],
			module_list: [],
			ViewVisible: false,
			runSuiteForm: {
				env_id: '',
				suite_id: '',
			},
			SuiteSave:{
				project: '',
				name: '',
				conditions: [],
			},
			role_names: [],
		}
	},
	setup() {
		return {
			Edit,
			Delete,
			Plus,
			View,
			EditPen,
			Cpu
		}
	},
	methods:{
		...mapActions(['getRolePermission']),
		run(obj){
			if(!this.env_id){
				ElMessage({message: '请选择执行环境', type: 'error'})
			}else{
				this.runSuite(obj)
			}
		},
		async runSuite(obj){
			this.runSuiteForm.env_id = this.env_id
			this.runSuiteForm.suite_id = obj.id
			const response = await this.$api.runSuite(this.runSuiteForm)
			ElMessage({message: "【" + obj.name + '套件】' + '执行中，可在报告模块查看进度', type: 'success'})
		},
		handleCurrentChange(){
			this.getSuites()
		},
		handleSizeChange(){
			this.getSuites()
		},
		search(){
			this.getSuites()
		},
		reset(){
			for(let key in this.suiteSearch){
				this.suiteSearch[key] = ''
			}
		},
		editSuite(row_data){
			this.isAdd = false
			this.title = '编辑套件'
			this.getSuite(row_data.id)
			this.editDialogVisible = true
		},
		addSuite(){
			this.title = '新增套件'
			this.editDialogVisible = true
			this.isAdd = true
		},
		save(){
			if (this.isAdd){
				this.createSuite()
			}else{
				this.updateSuite()
			}
		},
		async createSuite(){
			this.SuiteSave.project = this.projectInfo.id
			const response = await this.$api.createSuite(this.SuiteSave)
			if(response.status === 201){
				this.editDialogVisible = false
				this.getSuites()
			}
		},
		async updateSuite(){
			this.SuiteSave.project = this.projectInfo.id
			const response = await this.$api.updateSuite(this.SuiteSave.id, this.SuiteSave)
			if(response.status === 200){
				this.editDialogVisible = false
				this.getSuites()
			}
		},
		async deleteSuite(id){
			const response = await this.$api.deleteSuite(id)
			if (response.status === 204){
				this.getSuites()
			}
		},
		async getSuite(id){
			const response = await this.$api.getSuite(id)
			if (response.status === 200){
				this.SuiteSave = {...response.data.result}
			}
		},
		async getSuites(){
			this.suiteSearch.project = this.projectInfo.id
			this.SuiteSave.project = this.projectInfo.id
			const response = await this.$api.getSuites(Object.assign(this.suiteSearch, this.page_size_params))
			if (response.status === 200){
				this.suite_list = {...response.data}
			}
		},
	},
	created() {
		this.getRolePermission(this.pathPermission[this.$route.path]).then(res =>{
			this.permission = {...res.result}
		})
		this.getSuites()
		
	}
}
</script>

<style scoped>
	.suite{
		width: 100%;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}
	.suite .searchForm{
		width: 100%;
		flex: 1;
		background-color: white;
		padding: 15px 15px 0 15px;
		margin-bottom: 5px;
	}
	.suite .el-form--inline .el-form-item{
		margin-bottom: 15px;
	}
	.suite .table{
		background-color: white;
		flex: 1;
	}
	.searchForm .el-input{
		width: 150px;
	}
	.searchForm .el-select{
		width: 150px;
	}
	.dialog .el-input{
		width: 300px;
	}
	.dialog .el-select{
		width: 300px;
	}
	/deep/ .dialog .el-input__wrapper{
		width: 270px;
	}
	.form .el-select{
		width: 300px;
	}
	.el-button{
		width: 70px;
	}
	.el-icon{
		margin-left: 10px;
	}
	
</style>
